<?php

namespace dlApi\modules\product\controllers;

use common\components\fast_api\Controller;
use common\datas\Enums;
use dlApi\models\db\Spus;

class SpuController extends Controller
{
    /**
     * 首页商品列表
     * @desc 首页商品列表
     * @param int $page 页码
     * @param int $pageSize 每页数量
     * @param int $selCateRootId 根分类id
     * @return mixed
     * @throws \yii\base\InvalidConfigException
     */
    public function actionSiteList(int $page = 1, int $pageSize = 10, int $selCateRootId = 0)
    {
        $query = Spus::find()->alias('spu');
        $query->with(['skus' => function($q) {
            $q->alias('sku');
            $q->orderBy(['sku.sort' => SORT_ASC]); // 排序最小的sku
            $q->with(['coverFile' => function($q1) {
                $q1->alias('coverFile');
            }]);
        }]);
        $query->andFilterWhere(['spu.status' => Enums::ACTIVE]);
        $query->andFilterWhere(['spu.is_put_on_sale' => Enums::YES]);
        $query->orderBy(['spu.id' => SORT_DESC]);
        if ($selCateRootId > 0) {
            $query->andFilterWhere(['spu.cate_root_id' => $selCateRootId]);
        }
        $apiPageData = $this->apiPageData($query, $page, $pageSize);
        $items = [];
        foreach ($apiPageData->dataProvider->models as $k => $v) {
            /** @var Spus $v */
//            $m = $v->toArray();
            $sku = $v->skus[0];
            $m['_spu_id'] = $v->id;
//            $m['_spu_created_at'] = date("Y-m-d H:i", $v->created_at);
//            $m['_spu_sort'] = $v->sort;
            $m['_spu_name'] = $v->name;
            $m['_spu_numbering'] = $v->numbering;
//            $m['_spu_inventory'] = $v->inventory;
            $m['_sku_sort'] = $sku->sort;
            $m['_sku_id'] = $sku->id;
            $m['_sku_cover_url'] = $sku->coverFile?$sku->coverFile->abUrl:'';
            $m['_sku_inventory'] = $sku->inventory;
            $m['_sku_cost_price'] = $sku->cost_price;
            $m['_sku_commission_price'] = $sku->commission_price;
            $m['_sku_shop_price'] = $sku->shop_price;
            $m['_sku_sale_price'] = $sku->sale_price;
            $m['_sku_guide_price'] = $sku->guide_price;
            $m['_sku_reward_price'] = $sku->reward_price;
            $m['_sku_sales_volume'] = $sku->sales_volume;
            $items[] = $m;
        }
        $data = [
            'items' => $items,
            '_meta' => $apiPageData->meta,
        ];
        return $this->apiSuccess($data, '获取商品列表成功');
    }

    /**
     * 商品详情
     * @desc 商品详情
     * @param int $spuId 商品spuId
     * @return mixed | array
     * @throws \yii\base\InvalidConfigException
     */
    public function actionDetail(int $spuId)
    {
        $query = Spus::find()->alias('spu');
        $query->andWhere(['spu.id' => $spuId]);
        $query->with(['skus' => function($q) {
            $q->alias('sku');
            $q->with(['coverFile' => function($q1) {
                $q1->alias('coverFile');
            }, 'hasAttrvs' => function($q2) {
                $q2->alias('hasAttrvs');
                $q2->with(['skuAttrv' => function($q3) {
                    $q3->alias('skuAttrv');
                    $q3->with(['skuAttrk' => function($q4) {
                        $q4->alias('skuAttrk');
                    }]);
                }]);
            }]);
        }, 'remoteFreights' => function($q) {
            $q->alias('remoteFreights');
            $q->with(['province' => function($q1) {
                $q1->alias('province');
            }]);
        }, 'hasKeywords' => function($q) {
            $q->alias('hasKeywords');
            $q->with(['searchKeyword' => function($q1) {
                $q1->alias('searchKeyword');
            }]);
        }, 'category' => function($q) {
            $q->alias('category');
        }, 'supplier' => function($q) {
            $q->alias('supplier');
        }]);
//        $Spu = $query->one();
        $Spu = $query->asArray()->one();
        return $this->apiSuccess($Spu);
        if (!$Spu) {
            return $this->apiError('未找到商品');
        }
        if ($Spu->is_put_on_sale !== Enums::YES) {
            return $this->apiError('商品已下架');
        }
        $detail = $Spu->toArray();
        return $this->apiSuccess([
            'detail' => $detail,
        ]);
    }
}